'use strict';

define(function(require,exports,module){
	
	var React = require('react');
	var $ = require('jquery');
	var moment = require('moment');

	var publicObj = require('lib/public');
	var token = publicObj.get_token();
	
	/*DATE下拉框*/
	var SelectOptionDate = require('components/partial/SelectOptionDate');
	
	//折线图
	var AccessLogLineDiagram = require('components/diagram/DayOpenLineDiagram');

    var AccessLogMuliti = require('components/diagram/SplineDiagram/AccessLogMuliti');

    //TimeBucket
    var AccessLogTimeBucket = require('./AccessLogDiagram/TimeBucket');
	
	module.exports = React.createClass({
		handleDateChange:function(event){
			var date = event.target.value;
			this.setState({'date':date});
            this.setState({'hardware':''});
			this.loadData(date,'');
		},
		handleHardwareChange:function(event){
			var hardware = event.target.value;
			this.setState({'hardware':hardware});
			this.loadData(this.state.date,hardware);
		},
		loadData:function(date,hardware){
			
			this.setState({data:[]});
			if(date){

                var sql;

                sql = "SELECT count(*) as value,\
							thehour as time\
							FROM qeeniao.access_log\
							where thedate = "+date+" \
							group by time\
							order by time ASC";

				$.ajax({
					url:this.props.url,
					data:{sql:sql,'access_token':token},
					type:'post',
					dataType:'json',
					success:function(o){
						var tmp = [];
						tmp.push(o);
						if(o){
							if(this.isMounted()) {
								this.setState({'data': tmp});
							}
						}
					}.bind(this),
					error:function(xhr,status,err){
						console.error(sql,status,err.toString());
					}.bind(this)
				});
			}
		},
		getInitialState:function(){
			return {
				'data':[],
                'all_data':{'ios':[],'android':[],'all':[]},
				'date':'',
				'ajax_data':{'access_token':token}
			}
		},
		render:function(){
            var diagram;

            if(this.state.data.length > 0){
                diagram = this.state.data.map(function(item){
                    return (
                        <AccessLogLineDiagram className="" id="AccessLog" id_name="AccessLog" title="单日统计次数" yName="统计次数" yUnit="（时/次数）" url="" data={item} />
                    );
                });
            }else{
				diagram = <AccessLogMuliti className="" id="AccessLogAreaBasic" id_name="AccessLogAreaBasic" y="统计次数" title="每日统计次数" ajax_data={this.state.ajax_data} url={this.props.url} interval="300000"/>;
            }


			return (
				<div className="col-xs-6">
					<div className="row bgwhite">
						<div className="col-xs-4 bs-callout bs-callout-info">
							<h4>{this.props.title}: </h4>
						</div>
						<div className="col-xs-4 ">
							<SelectOptionDate handleDateChange={this.handleDateChange} data={this.props.data} />
						</div>
						<div className="col-xs-2" style={{margin:'4px 0'}}>
							<button onClick={this.createHandle} className="btn btn-primary btn" data-toggle="modal" data-target="#AccessLogTimeBucket">更多</button>
						</div>
                        <AccessLogTimeBucket id="AccessLogTimeBucket" url={this.props.url}/>
					</div>
					<div className="row">
					{diagram}
					</div>
				</div>
			);
		}
	});

});